#include "robot.h"
#include "Thread_Class/robotgraplord.h"
#include "Game_Control_Class/strategy.h"
#include "Thread_Class/robotplayhand.h"
#include <QDebug>

Robot::Robot(QObject *parent)
    : Player{parent}
{
    m_type=Player::Robot;
}

void Robot::prepareCallLord()
{
    RobotGrapLord* subThread=new RobotGrapLord(this);
    connect(subThread,&RobotGrapLord::finished,this,[=]{
        qDebug()<<"RobotGrapLord子线程对象析构"<<",Robot name:"<<this->getName();
        subThread->deleteLater();
    });
    subThread->start();
}

void Robot::preparePlayHand()
{
    RobotPlayHand* subThread=new RobotPlayHand(this);
    connect(subThread,&RobotGrapLord::finished,this,[=]{
        qDebug()<<"RobotGrapLord子线程对象析构"<<",Robot name:"<<this->getName();
        subThread->deleteLater();
    });
    subThread->start();
}

void Robot::thinkCallLord()
{
    /*
     * 基于手中的牌计算权重
     * 大小王：6
     * 顺子/炸弹：5
     * 三张点数相同的牌：4
     * 2的权重：3
     * 对牌：1
    */
    int weight=0;
    //qDebug()<<"??";
    Strategy st(this,m_cards);

    weight+=st.getRangeCards(Card::Card_SJ,Card::Card_BJ).cardCount()*6;

    QVector<Cards> optSeq= st.pickOptimalSeqSingles();
    //qDebug()<<"??";
    weight+=optSeq.size()*5;

    QVector<Cards> Bombs=st.findCardsByCount(4);
    weight+=Bombs.size()*5;

    weight+=m_cards.pointCount(Card::Card_2)*3;

    Cards tmp=m_cards;
    tmp.remove(optSeq);
    tmp.remove(Bombs);

    Cards card2=st.getRangeCards(Card::Card_2,Card::Card_2);
    tmp.remove(card2);

    QVector<Cards> Triples=Strategy(this,tmp).findCardsByCount(3);
    weight+=Triples.size()*4;

    tmp.remove(Triples);

    QVector<Cards> pairs=Strategy(this,tmp).findCardsByCount(2);
    weight+=Triples.size()*1;

    //qDebug()<<weight;
    if(weight>=22)
    {
        grabLordBet(3);
    }
    else if(weight<22&&weight>=18)
    {
        grabLordBet(2);
    }
    else if(weight<18&&weight>=10)
    {
        grabLordBet(1);
    }
    else
    {
        grabLordBet(0);
    }

}

void Robot::thinkPlayHand()
{
    Strategy st(this,m_cards);
    Cards cs=st.makeStrategy();
    playHand(cs);
}
